$(function () {
    /**获取统计信息**/
    $.post(ctx+"SalePhone/getStatisTaskByBatch",{uuid:$("#selectUUids").val()},function (r) {
        if (r.code == 0){var data = r.msg;$("#numfen").text(data.spStatusFen);}
    });
    $.post(ctx+"dept/getUseeAndDepttree",{},function (r) {
        var array  = r.msg;
        $("#using_json").jstree({
            "core": {
                "multiple": true,//单选
                "data":array
            },
            "checkbox" : {
                "keep_selected_style" : false
            },
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#using_json').on("changed.jstree", function (e, data) {
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;;
            if (action == "deselect_node") {
                $("#user-selectfor").find("div").each(function () {
                    var data_id = $(this).attr("data-id");
                    if (data_id == nodeid) $(this).remove();
                });
            }else{
                var length = $("#user-selectfor").find("div").length;
                if (length >=10){$JuXue.n_danger("选择员工不能超过10人");return}
                var label = "<div class='label label-danger p-3 m-3 pull-left' data-id='"+nodeid+"'>"+text+"</div>";
                $("#user-selectfor").append(label);
            }
        });
    });
});
/**选择人员**/
function selectUsers(){
    $("#selectUser").modal("hide");
    var alldiv = $("#user-selectfor").find("div");
    /**查找num**/
    var num = $("#numfen").text();
    var length  =  alldiv.length;
    var fenpeo= Math.floor(num /length);
    var fenpshu= num % length;
    var yifn = 0;
    $("#selectFeUse").empty();
    $.each(alldiv,function (index,val) {
       var userid  = $(this).attr("data-id");
       var usrname = $(this).text();
       if (index < fenpshu) {
           var val = fenpeo+1;
           yifn += val;
           var div = "<div class=\"col-md-10 form-group m-b-3\" data='"+userid+"'>\n" +
               "   <label class=\"col-md-3  control-label text-left\">"+usrname+"</label>\n" +
               "   <div class=\"col-md-5 m-t-5 input-group\">\n" +
               "       <input type=\"text\" class=\"form-control height-30\" value="+val+" onchange='changeinput()' />\n" +
               "     </div>\n" +
               " </div>";
            $("#selectFeUse").append(div);
       }else{
           if (yifn >=num){
               var div = "<div class=\"col-md-10 form-group m-b-3\" data='"+userid+"' >\n" +
                   "   <label class=\"col-md-3  control-label text-left\">"+usrname+"</label>\n" +
                   "   <div class=\"col-md-5 m-t-5 input-group\">\n" +
                   "       <input type=\"text\" class=\"form-control height-30\" value=\"0\" onchange='changeinput()' />\n" +
                   "     </div>\n" +
                   " </div>";
               $("#selectFeUse").append(div);
           }else{
               yifn += fenpeo;
               var div = "<div class=\"col-md-10 form-group m-b-3\" data='"+userid+"'>\n" +
                   "   <label class=\"col-md-3  control-label text-left\">"+usrname+"</label>\n" +
                   "   <div class=\"col-md-5 m-t-5 input-group\">\n" +
                   "       <input type=\"text\" class=\"form-control height-30\" value="+fenpeo+" onchange='changeinput()'/>\n" +
                   "     </div>\n" +
                   " </div>";
               $("#selectFeUse").append(div);
           }
       }
    });
}
/**可分配 选择**/
function changeinput(){
    var num = $("#numfen").text();
    var allnum = 0;
    $("#selectFeUse").find("input").each(function () {
       var ids = $(this).val();
        allnum += parseInt(ids);
    });
    if (num < allnum) {
        $JuXue.n_danger("没有可支配额度");
    }
}
/**确定选择**/
function queindexUser(){
     var num = $("#numfen").text();
    var allnum = 0;
    var flag = false;
    $("#selectFeUse").find("input").each(function () {
        var ids = $(this).val();
        var data = parseInt(ids);
        if (data <=0){flag = true;return false;}
        allnum +=data;
    });
    if (flag || num <allnum){$JuXue.n_danger("支配额度超出了可执行范围");
    }else{
        if(num != allnum){
            $JuXue.n_danger("支配额度尚有余额");
        }else{

            var uuid = $("#selectUUids").val();
            var userids = new Array();
            $("#selectFeUse").children("div").each(function () {
               var user = $(this).attr("data");
                userids.push(user);
            });
            var nums = new Array();
            $("#selectFeUse").find("input").each(function () {
                var num = $(this).val();
                nums.push(num);
            });
            if (nums.length != userids.length){$JuXue.n_danger("数据获取失败");return}

            var rece = userids.join(",");
            var numall = nums.join(",");
            $.post(ctx+"SalePhone/FenTaskByBatch",{"spUuid":uuid,"spRecer":rece,"spFenName":numall},function (r) {
                if (r.code == 0){
                    $JuXue.n_success("分配成功");
                    $('#diaLoadModal').empty().modal('hide');
                    init_create_data();
                }else{
                    $JuXue.n_danger("分配失败");
                }

            });

        }

    }

}


